<?php
/**
 * @version		$Id: tlds.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelTlds extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'TldId', 'a.TldId',
				'TldExtension', 'a.TldExtension',
				'TltPrice', 'a.TltPrice',
				'TltWhois', 'a.TltWhois',
				'TltAvailable', 'a.TltAvailable',
				'TltTaken', 'a.TltTaken',
				'TltRegistrant', 'a.TltRegistrant',
				'TldActive', 'a.TldActive',
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);
		
		
		parent::populateState('a.TldExtension', 'asc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_tlds AS a');
		
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.TldActive = '.$statusId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'TldExtension:') === 0) {
				$query->where('a.TldExtension = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.TldExtension LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.TldExtension')).' '.$db->getEscaped($this->getState('list.direction', 'asc')));
			
		return $query;		
	}

}